package cn.xiuxius.askbox.controller.console;


import cn.xiuxius.askbox.pojo.dto.add.AdminAddDTO;
import cn.xiuxius.askbox.pojo.dto.common.LoginDTO;
import cn.xiuxius.askbox.pojo.dto.query.AdminQueryDTO;
import cn.xiuxius.askbox.pojo.dto.update.AdminUpdateDTO;
import cn.xiuxius.askbox.pojo.dto.update.AdminUpdateStateDTO;
import cn.xiuxius.askbox.pojo.result.PageResult;
import cn.xiuxius.askbox.pojo.result.Result;
import cn.xiuxius.askbox.pojo.vo.AdminVO;
import cn.xiuxius.askbox.pojo.vo.LoginVO;
import cn.xiuxius.askbox.service.IConsoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoxiu
 * @since 2024-07-20
 */
@Api(tags = "后台-管理员接口")
@RestController("consoleAdminController")
@RequestMapping("/console/consoles")
public class ConsoleController {

    @Autowired
    private IConsoleService consoleService;

    @ApiOperation("控制台登录")
    @PostMapping("/login")
    public Result login(@Validated LoginDTO loginDTO){
        LoginVO vo = consoleService.login(loginDTO);
        return Result.success(vo);
    }

    @ApiOperation("添加管理员")
    @PostMapping("/admin")
    public Result addAdmin(@Validated @RequestBody AdminAddDTO dto){
        consoleService.addAdmin(dto);
        return Result.success();
    }

    @ApiOperation("分页查询管理员")
    @GetMapping("/admin")
    public Result getAdminsByPage(@Validated AdminQueryDTO queryDTO){
        PageResult pageResult = consoleService.getAdminsByPage(queryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("根据uuid查询管理员")
    @GetMapping("/admin/{uuid}")
    public Result getAdminByUuid(@PathVariable String uuid){
        AdminQueryDTO dto = new AdminQueryDTO();
        dto.setUuid(uuid);
        AdminVO vo = consoleService.getAdminByUuid(dto);
        return Result.success(vo);
    }

    @ApiOperation("修改管理员")
    @PutMapping("/admin")
    public Result updateAdmin(@Validated @RequestBody AdminUpdateDTO dto){
        consoleService.updateAdmin(dto);
        return Result.success();
    }

    @ApiOperation("修改管理员账户状态")
    @PutMapping("/admin/state")
    public Result updateAdminState(@Validated @RequestBody AdminUpdateStateDTO dto){
        consoleService.updateAdminState(dto);
        return Result.success();
    }

    @ApiOperation("删除管理员")
    @DeleteMapping("/admin/{uuid}")
    public Result deleteAdmin(@PathVariable String uuid){
        consoleService.deleteAdmin(uuid);
        return Result.success();
    }

}
